<template>
  <n-space vertical>
    <n-space>
      <SearchForm @handleSearch="handlePageQuery" :searchModel="pageQueryParam.param">
        <n-form-item label="任务名称:" path="jobName">
          <n-input round clearable placeholder="请输入任务名称" v-model:value="pageQueryParam.param.jobName"/>
        </n-form-item>
        <n-form-item label="调度名称:" path="scheduleName">
          <n-input round clearable placeholder="请输入调度名称" v-model:value="pageQueryParam.param.scheduleName"/>
        </n-form-item>
        <n-form-item label="调度时间:">
          <n-date-picker v-model:value="scheduleDateRange" type="datetimerange" clearable @update:value="setSearchDate"
                         @clear="clearScheduleDateRange"/>
        </n-form-item>
      </SearchForm>
    </n-space>

    <n-space>
      <PageTable @handlePageQuery="handlePageQuery" :columns="basicColumns" :loading="loading" :pageResult="pageResult"/>
    </n-space>

  </n-space>
</template>

<script lang="ts" setup>
import {reactive, onMounted, toRefs} from "vue";
import ScheduleLogApi from "../api/ScheduleLogApi";
import {PageQuery, PageResult} from "../model/Common";
import SearchForm from "../components/table/SearchForm.vue";
import PageTable from "../components/table/PageTable.vue";
import {JobRunningVO, PageQueryRunningParam, basicColumns} from "../model/Running";
import {dayStart, nextDayStart} from "../util/utils";

const data = reactive({
  pageQueryParam: new PageQuery<PageQueryRunningParam>(new PageQueryRunningParam()),
  pageResult: new PageResult<JobRunningVO>(),
  loading: false,
  scheduleDateRange: [dayStart(), nextDayStart()]
});

const setSearchDate = (value: [number, number]): void => {
  data.pageQueryParam.param.scheduleTimeStart = value[0];
  data.pageQueryParam.param.scheduleTimeEnd = value[1];
};

const clearScheduleDateRange = (): void => {
  data.pageQueryParam.param.scheduleTimeStart = undefined;
  data.pageQueryParam.param.scheduleTimeEnd = undefined;
};

const handlePageQuery = (page?: number, size?: number): void => {
  if (page) {
    data.pageQueryParam.page = page;
  }
  if (size) {
    data.pageQueryParam.size = size;
  }
  data.loading = true;
  ScheduleLogApi.pageList(data.pageQueryParam)
      .then(response => {
        if (response) {
          data.pageResult = response;
        }
        data.loading = false;
      });
};

const {pageQueryParam, pageResult, loading, scheduleDateRange} = toRefs(data);

onMounted(() => {
  handlePageQuery();
});
</script>
